package com.netease.financial.data.repository.datasource;

import com.netease.financial.data.cache.NfsCache;
import com.netease.financial.data.entity.AccessTokenCheckEntity;
import com.netease.financial.data.entity.ActivityPageEntity;
import com.netease.financial.data.entity.AppUpdateEntity;
import com.netease.financial.data.entity.LaunchPageEntity;
import com.netease.financial.data.entity.LoginEntity;
import com.netease.financial.data.entity.LogoutEntity;
import com.netease.financial.data.entity.PageInfoEntity;
import com.netease.financial.data.entity.RegisterEntity;
import com.netease.financial.data.entity.UnreadMessageCountEntity;
import com.netease.financial.data.net.RestApi;
import com.netease.financial.domain.interactor.RegisterInput;

import java.util.List;

import rx.Observable;
import rx.functions.Action1;

/**
 * {@link NfsDataStore} implementation based on connections to the api (Cloud).
 */
public class CloudNfsDataStore implements NfsDataStore {

    private final RestApi mRestApi;
    private final NfsCache mNfsCache;

    /**
     * Construct a {@link NfsDataStore} based on connections to the api (Cloud).
     *
     * @param restApi  The {@link RestApi} implementation to use.
     * @param nfsCache A {@link NfsCache} to cache data retrieved from the api.
     */
    public CloudNfsDataStore(RestApi restApi, NfsCache nfsCache) {
        mRestApi = restApi;
        mNfsCache = nfsCache;
    }

    @Override
    public Observable<AppUpdateEntity> getAppUpdateInfo(final String device, final int versionCode) {
        return mRestApi.getAppUpdateInfo(device, versionCode).doOnNext(new Action1<AppUpdateEntity>() {
            @Override
            public void call(AppUpdateEntity entity) {
                if (entity != null) {
                    // Cache AppUpdateEntity
                    CloudNfsDataStore.this.mNfsCache.putAppUpdateEntity(entity);
                }
            }
        });
    }

    @Override
    public Observable<UnreadMessageCountEntity> getUnreadMessageCount(String accessToken) {
        return mRestApi.getUnreadMessageCount(accessToken);
    }

    @Override
    public Observable<LoginEntity> login(String clientId, String clientSecret, String appSecret) {
        return mRestApi.login(clientId, clientSecret, appSecret);
    }

    @Override
    public Observable<LogoutEntity> logout(String accessToken, String appSecret) {
        return mRestApi.logout(accessToken, appSecret);
    }

    @Override
    public Observable<AccessTokenCheckEntity> checkAccessToken(String accessToken, String appSecret) {
        return mRestApi.checkAccessToken(accessToken, appSecret);
    }

    @Override
    public Observable<ActivityPageEntity> getActivityPageInfo() {
        return mRestApi.getActivityPageInfo();
    }

    @Override
    public Observable<LaunchPageEntity> getLaunchPageInfo(int widthPixels, int heightPixels, String platform) {
        return mRestApi.getLaunchPageInfo(widthPixels, heightPixels, platform);
    }

    @Override
    public Observable<RegisterEntity> register(RegisterInput registerInput) {
        return mRestApi.register(registerInput);
    }

    @Override
    public Observable<PageInfoEntity> getPageInfo(String pageName, List<String> elements, int widthPixels, int heightPixels, String platform) {
        return mRestApi.getPageInfo(pageName, elements, widthPixels, heightPixels, platform);
    }

}
